let jwt = require('./jwt');

module.exports = (req, res, next) => {

  //处理默认的公共参数，address || !addess || headers
  req.query._page = req.query._page ? req.query._page - 1 : require('../config/gobal')._page;
  req.query._limit = req.query._limit ? req.query._limit - 0 : require('../config/gobal')._limit;
  req.query.q = req.query.q ? req.query.q : require('../config/gobal').q;
  req.query._sort = req.query._sort ? req.query._sort : require('../config/gobal')._sort;

  req.body._page = req.body._page ? req.body._page - 1 : require('../config/gobal')._page;
  req.body._limit = req.body._limit ? req.body._limit - 0 : require('../config/gobal')._limit;
  req.body.q = req.body.q ? req.body.q : require('../config/gobal').q;
  req.body._sort = req.body._sort ? req.body._sort : require('../config/gobal')._sort;

  req.headers._page = req.headers._page ? req.headers._page - 1 : require('../config/gobal')._page;
  req.headers._limit = req.headers._limit ? req.headers._limit - 0 : require('../config/gobal')._limit;
  req.headers.q = req.headers.q ? req.headers.q : require('../config/gobal').q;
  req.headers._sort = req.headers._sort ? req.headers._sort : require('../config/gobal')._sort;

  //处理 身份验证

  if(/login|reg/.test(req.url)){
    next()
  }else{

    //校验token

    let token = req.headers.token || req.query.token || req.body.token;

    jwt.verify(token).then(
      decode => {
        req.query.decode=decode;
        next()
      }
    ).catch(
      message => res.send({
        err:2,
        msg:'token过期或者失效' + message
      })
    )

  }


  //处理跨域

  
}